package com.qiku.controller;

import com.qiku.domain.Course;
import com.qiku.domain.CourseVo;
import com.qiku.domain.ResponseResult;
import com.qiku.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 多条件查询
     * @param courseVo
     * @return
     */
    @PostMapping("/findAllCourse")
    //@RequestBody  把对象转化为JSON串
    public ResponseResult findCourseByCondition(@RequestBody CourseVo courseVo) {
        //查询课程
        List<Course> courseList = courseService.findCourseByCondition(courseVo);
        //封装结果集
        ResponseResult result = null;
        if (null != courseList) {
            result = new ResponseResult(true, 200, "响应成功~", courseList);
        } else {
            result = new ResponseResult(false, 200, "查询结果为空！", null);
        }
        return result;
    }

    /**
     * 文件上传
     * @param file
     * @param request
     * @return
     */
    @PostMapping("/courseUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            // 判断文件是否为空
            if (file.isEmpty()) {
                throw new RuntimeException("未找到图片信息!");
            }
            // 获取项目部署路径
            String realPath = request.getServletContext().getRealPath("/");
            System.out.println("获取项目部署路径realPath:" + realPath);
            String projectName = request.getContextPath().substring(1);
            System.out.println("获取项目名projectName:" + projectName);
            String webappPath = realPath.substring(0, realPath.indexOf(projectName));
            String imgPath = webappPath + "/upload/";
            // 获取源文件名
            String filename = file.getOriginalFilename();
            // 设置新文件名
            String newFileName = System.currentTimeMillis() + filename;
            // 上传文件
            File filePath = new File(imgPath, newFileName);
            file.transferTo(filePath);
            // 返回结果集
            HashMap<String, String> map = new HashMap<>();
            map.put("filename", newFileName);
            map.put("filePath", imgPath + newFileName);
            ResponseResult result = new ResponseResult(true, 200, "响应成功", map);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存或修改课程信息
     * @param courseVo
     * @return
     */
    @PostMapping("/saveOrUpdateCourse")
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVo courseVo) {
        ResponseResult result = null;
        try {
            //根据参数中是否含有id属性   判断新增 或 修改
            if (null != courseVo.getId() && 0 < courseVo.getId()) {
                //id合法   修改操作
                courseService.updateCourseOrTeacher(courseVo);
                result = new ResponseResult(true, 200, "修改成功~", null);
            } else {
                courseService.saveCourseOrTeacher(courseVo);
                result = new ResponseResult(true, 200, "保存成功~", null);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生异常~", null);
            return result;
        }
    }

    /**
     * 根据id查询课程信息  回显课程信息与讲师信息
     *
     * @param id
     * @return
     */
    @GetMapping("/findCourseById")
    public ResponseResult findCourseById(@RequestParam Integer id) {
        CourseVo courseVo = courseService.findCourseById(id);
        //封装结果集
        ResponseResult result = null;
        if (null != courseVo) {
            result = new ResponseResult(true, 200, "响应成功~", courseVo);
        } else {
            result = new ResponseResult(false, 500, "查询结果为空！", courseVo);
        }
        return result;
    }

    /**
     *  修改课程状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(int id, int status) {
        ResponseResult result = null;
        Map<String, Integer> map = null;
        try{
            courseService.updateCourseStatus(id, status);
            map = new HashMap<>();
            map.put("status", status);
            result = new ResponseResult(true, 200, "响应成功~", map);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生错误~", map);
            return result;
        }
    }

}
